<?php

namespace App\models;

/**
 *
 */
class User extends \App\base\Model
{
    const _auth = 'user_auth';

    public function getId($params)
    {
      return $this->_db->get(self::_auth,['uid'],[
        "OR" => [
          'user_login' => $params,'register_ip' => $params
        ]
        ])['uid'];
    }

    public function getInfo($token)
    {
      return $this->_db->get(self::_auth,[
        'uid','user_login','login_ip','register_ip','create_at','update_at'
      ],[
        'access_token' => $token
      ]);
    }

    public function generateToken($username)
    {
      $token = md5($username.time());
      $this->_db->update(self::_auth,[
        'access_token' => $token
      ],[
        'user_login' => $username
      ]);
      return $token;
    }

    public function validateToken($token)
    {
      return $this->_db->has(self::_auth,['access_token' => $token]);
    }

    public function validatePassword($username,$password)
    {
      $password_hash = $this->_db->get(self::_auth,[
        'password'
      ],[
        'user_login' => $username,
        'status' => 1
      ])['password'];
      return password_verify($password,$password_hash);
    }
}
